Enter the numerator: Rats If the division didn't work, you entered bad data. Exception in thread "main" java.lang.NumberFormatException: Rats at java.lang.Integer.parseInt(Integer.java:409) at java.lang.Integer.parseInt(Integer.java:458) at FinallyPractice.main(FinallyPractice.java:16)
The try{}
block threw a NumberFormatException
,
but there was no catch{}
block for it.
So: (1) the finally
block executed, and then
(2) the execption was thrown to the caller (in this case the Java run time system),
which (3) printed the last four lines.
The last four lines are called a stack trace. Later you will see how to print them without stopping the program. Here is a section of the program:
try { System.out.println("Enter the numerator:"); ... System.out.println("Enter the divisor:"); ... System.out.println( num + " / " + div + " is " + (num/div) ); } catch (ArithmeticException ex ) { System.out.println("You can't divide " + num + " by " + div); } finally { System.out.println("If the division didn't work, you entered bad data." ); } System.out.println("Good-by" );
Here are some further examples of output:
Output 1:Enter the numerator: 26 Enter the divisor: 4 26 / 4 is 6 If the division didn't work, you entered bad data. Good-byOutput 2:Enter the numerator: 26 Enter the divisor: 0 You can't divide 26 by 0 If the division didn't work, you entered bad data. Good-byOutput 3:Enter the numerator: 26 Enter the divisor: Zero If the division didn't work, you entered bad data. Exception in thread "main" java.lang.NumberFormatException: Zero at java.lang.Integer.parseInt(Integer.java:409) at java.lang.Integer.parseInt(Integer.java:458) at FinallyPractice.main(FinallyPractice.java:19)